package com.example.qu_tao.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.qu_tao.R;

import java.io.InputStream;
import java.util.ArrayList;

public class FaDongTaiAdapter extends RecyclerView.Adapter<FaDongTaiAdapter.ImageViewHolder> {

    public interface OnImageDeleteListener {
        void onImageDelete(int position);
    }

    private Context context;
    private ArrayList<Uri> images;
    private OnImageDeleteListener deleteListener;

    public FaDongTaiAdapter(Context context, ArrayList<Uri> images, OnImageDeleteListener listener) {
        this.context = context;
        this.images = images;
        this.deleteListener = listener;
    }

    @NonNull
    @Override
    public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context)
                .inflate(R.layout.item_fadongtai_image, parent, false);
        return new ImageViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ImageViewHolder holder, int position) {
        new LoadImageTask(holder.imageView).execute(images.get(position));

        holder.btnDelete.setOnClickListener(v -> {
            if (deleteListener != null) {
                deleteListener.onImageDelete(holder.getAdapterPosition());
            }
        });
    }

    @Override
    public int getItemCount() {
        return images.size();
    }

    static class ImageViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        ImageButton btnDelete;

        public ImageViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.iv_image);
            btnDelete = itemView.findViewById(R.id.btn_delete);
        }
    }

    private static class LoadImageTask extends AsyncTask<Uri, Void, Bitmap> {
        private ImageView imageView;

        public LoadImageTask(ImageView imageView) {
            this.imageView = imageView;
        }

        @Override
        protected Bitmap doInBackground(Uri... uris) {
            try {
                InputStream is = imageView.getContext().getContentResolver().openInputStream(uris[0]);
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 2; // 压缩图片
                return BitmapFactory.decodeStream(is, null, options);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            if (bitmap != null) {
                imageView.setImageBitmap(bitmap);
            } else {
                imageView.setImageResource(R.mipmap.fadongtai);
            }
        }
    }
}